package com.hongxun.design.adapters;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.activitys.EncyclopediasActivity;
import com.hongxun.design.bean.BuyMenuBean;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.dialogstyle.MyDialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zerdoor_pc on 2016/8/5.
 */
public class BuyMenuListAdapter extends BaseAdapter {
    Activity context;
    List<BuyMenuBean> beans;
    LayoutInflater inflater;
    Handler UIHandler;
    public Map<Integer, Boolean> map;
    int type;
    public BuyMenuListAdapter(Activity context, List<BuyMenuBean> beans, Handler UIHandler) {
        this.context = context;
        this.beans = beans;
        this.UIHandler = UIHandler;
        inflater = LayoutInflater.from(context);
        map = new HashMap<>();
        setMap();
    }

    public void setMap(){
        for (int i = 0; i < beans.size(); i++) {
            map.put(i, false);
        }
    }

    public void setType(int type){
        this.type = type;
    }
    public BuyMenuBean getBean(){
        return beans.get(currPosition);
    }
    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int i) {
        return beans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        BuyMenuBean bean = (BuyMenuBean) getItem(i);
        Holldler holldler = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_buymenu, null);
            holldler = new Holldler(view);
            view.setTag(holldler);
        } else {
            holldler = (Holldler) view.getTag();
        }

        try {
            holldler.title.setText(bean.getName());
            holldler.price.setText("￥" + bean.getProducts_price());
            holldler.vaild_time.setText(bean.getVaild_time() + getUnit(bean.getTime_unit(),holldler.vaild_time));
            holldler.illustrate.setOnClickListener(new MClickListener(i, bean) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    BuyMenuBean bean = (BuyMenuBean) msg;
                    Bundle bundle = new Bundle();
                    bundle.putString(MString.CODE, bean.getBuy_menu_id() + "");
                    bundle.putString(MString.TITLE, bean.getName());
                    bundle.putString(MString.CONTENT, bean.getIllustrate());
                    bundle.putString(MString.TYPE, MString.AUTHORIZATION);
                    IntentToActivity.intent(context, EncyclopediasActivity.class, bundle);
                }
            });

            holldler.def_people_num.setText("初始授权名额:" + bean.getDef_people_num() + "人");

            holldler.per_averages.setText("额外增加:");
            holldler.per_averages.append("￥" + bean.getPer_averages());
            holldler.per_averages.append("/人");

            holldler.check.setOnClickListener(new MClickListener(i, bean) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    if (currPosition==position)return;
                    currPosition=position;
                    SetTypeface.set(((TextView) view),IconSourse.checked);
                    UIHandler.sendEmptyMessage(1);
                    notifyDataSetChanged();
                }
            });
            if (currPosition==i) {
                SetTypeface.set(holldler.check, IconSourse.checked);
                if (type==2){
                    holldler.bottom_item.setVisibility(View.VISIBLE);
                    long addnum=bean.getAddNum();
                    if (addnum>9999){
                        holldler.buy_addnum.setText("9999+");
                    }else{
                        holldler.buy_addnum.setText(bean.getAddNum() + "");
                    }
                    holldler.buy_addnum.setOnClickListener(new MClickListener(i,holldler,bean) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            MyDialog.dialogForAuthorEdieText(context, msg, msg2,  UIHandler);
                        }
                    });
                    holldler.buy_reduce.setOnClickListener(new MClickListener(i, bean, holldler) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            BuyMenuBean bean = (BuyMenuBean) msg;
                            Holldler holldler = (Holldler) msg2;
                            long num = bean.getAddNum();
                            if (num != 0) {
                                bean.setAddNum(num - 1);
                                UIHandler.sendEmptyMessage(1);
                                notifyDataSetChanged();
                            }
                        }
                    });

                    holldler.buy_add.setOnClickListener(new MClickListener(i, bean, holldler) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            BuyMenuBean bean = (BuyMenuBean) msg;
                            Holldler holldler = (Holldler) msg2;
                            long num = bean.getAddNum();
                            if (num<9999){
                                bean.setAddNum(num + 1);
                                UIHandler.sendEmptyMessage(1);
                                notifyDataSetChanged();
                            }

                        }
                    });
                }else{
                    holldler.bottom_item.setVisibility(View.GONE);
                }

                UIHandler.sendEmptyMessage(1);
            } else {
                holldler.bottom_item.setVisibility(View.GONE);
                SetTypeface.set(holldler.check,IconSourse.unchecked);
            }
        } catch (Exception e) {
        }

        return view;
    }
    public int currPosition=0;

    private String getUnit(int type,TextView view) {
        String s = "";
        switch (type) {
            case 1:
                s += "日";
                view.setBackgroundResource(R.drawable.circular3_dayblue_item);
                break;
            case 2:
                s += "个月";
                view.setBackgroundResource(R.drawable.circular3_blue_item);

                break;
            case 3:
                s += "年";
                view.setBackgroundResource(R.drawable.circular3_yearblue_item);
                break;
        }
        return s;
    }

   public class Holldler {
       public TextView check, title, price, vaild_time, illustrate, def_people_num, per_averages, buy_reduce, buy_addnum, buy_add;
       public LinearLayout bottom_item;
       public  Holldler(View v) {
            check = (TextView) v.findViewById(R.id.buy_check);//选择
            title = (TextView) v.findViewById(R.id.buy_title);//标题
            price = (TextView) v.findViewById(R.id.buy_price);//价格
            vaild_time = (TextView) v.findViewById(R.id.buy_vaild_time);//有效期
            illustrate = (TextView) v.findViewById(R.id.illustrate);//说明
            bottom_item = (LinearLayout) v.findViewById(R.id.buy_bottom_item);
            def_people_num = (TextView) v.findViewById(R.id.def_people_num);//初始
            per_averages = (TextView) v.findViewById(R.id.per_averages);//额外增加
            buy_reduce = (TextView) v.findViewById(R.id.buy_reduce);//减
            buy_addnum = (TextView) v.findViewById(R.id.buy_addnum);//人数
            buy_add = (TextView) v.findViewById(R.id.buy_add);//加
            SetTypeface.set(check, IconSourse.unchecked);
            SetTypeface.set(illustrate, IconSourse.FORGETPASSWORD);
            illustrate.append("查看说明");
            SetTypeface.set(buy_reduce, IconSourse.reduce);
            SetTypeface.set(buy_add, IconSourse.add);
            bottom_item.setVisibility(View.GONE);
        }
    }
}
